package com.tianqu.tms.www.finance.controller;

import java.util.ArrayList;
import java.util.List;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.IntegralRegisterDto;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.finance.viewhelper.TransferFeeManagementViewHelper;

public class TransferFeeManagementSearchResultController extends TmsController {
	private Logger logger = Logger.getLogger(TransferFeeManagementSearchResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		try {
			TransferFeeManagementViewHelper view = (TransferFeeManagementViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			//
			Member member = new Member();
			Attn attn = new Attn();
			member = member.getIntegralManagementInfo(view.getAccessCardNo());
			attn = attn.getIntegralInfo(view.getAccessCardNo());
			int memberType = 0;
			String id = "0";
			//
			if(member!=null && !"2".equals(member.getUserId())){
				view.setMemberId(member.getMemId() + "");//会员编号
				view.setUserType("商户会员");//会员类型
				view.setCardScore(member.getCardScore());
				view.setRealName(member.getRealName());//会员名称
				view.setType(view.getType());//操作类型
				view.setVirtualMargin(StringUtil.getPointBetweenThree(Integer.valueOf(member.getVirtualMargin())));//剩余金额(元)
				id = member.getMemId()+"";
				memberType = 1;
			}else if(attn!=null&& !"2".equals(attn.getUserId())){
				view.setMemberId(attn.getAttnId() + "");//会员编号
				view.setUserType("经办人会员");//会员类型
				view.setCardScore(attn.getCardScore());
				view.setRealName(attn.getAttnName());//会员名称
				view.setType(view.getType());//操作类型
				view.setVirtualMargin(StringUtil.getPointBetweenThree(Integer.valueOf(attn.getVirtualMargin())));//剩余金额(元)
				id = attn.getAttnId()+"";
				memberType = 2;
			}
			if("1".equals(view.getType())){
				IntegralRegister integralRegister = IntegralRegister.getInstance();
				List<IntegralRegisterDto> list = integralRegister.findIntegralListByMemId(id, memberType, 10, 1);
				int i = 0;
				List<IntegralRegisterDto> r = new ArrayList<IntegralRegisterDto>();
				for (IntegralRegisterDto dto : list) {
					if(i>4) break;
					//
					if (i % 2 == 0) dto.setCss("1");
					else dto.setCss("2");
					//
					dto.setInsertTimeStr(StringUtil.getTimeByTimeStamp(dto.getInsertTime()));
					dto.setTransferPrices(StringUtil.getPointBetweenThree(dto.getTransferPrice()));
					dto.setNum(++i);
					r.add(dto);
				}
				view.setIntegralRegisterDtoList(r);
			}
			controller.commit();
			uc.setAttribute(TmsConstants.VIEWHELP, view);
		} catch (Exception e) {
			logger.error(e);
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
